// src/server/redis/index.ts
import { Models } from '@/common/typings/modle'
import Config from '@/config/Config'
import { DataBaseFailed } from '@/core/HttpException'
import redis from '@/server/redis/redis'

/**
 * redis报错回调
 * @param err
 */
export function redisCatch(err: Error) {
  throw new DataBaseFailed({ msg: err.message })
}

/**
 * 选择数据库
 * @param DbName
 * @returns
 */
export async function selectDb(DbName: number) {
  return new Promise((resolve) => {
    redis
      .select(DbName)
      .then(() => {
        resolve(true)
      })
      .catch(redisCatch)
  })
}

/**
 * 保存token
 * @param key
 * @param tokenInfo
 * @returns
 */
export async function saveToken(key: string, tokenInfo: string): Promise<Models.Result> {
  return new Promise((resolve) => {
    selectDb(Config.SECURITY.TOKEN_REDIS_DB).then(() => {
      redis.setex(key, Config.SECURITY.EXPIRES_IN, tokenInfo).then((res) => {
        const result: Models.Result = {
          msg: 'ok',
          state: 1,
          results: res,
          fields: [],
        }
        resolve(result)
      })
    })
  })
}
/**
 * 获取token的值
 * @param key
 * @returns
 */
export async function getTokenValue(key: string): Promise<Models.Result> {
  return new Promise((resolve) => {
    selectDb(Config.SECURITY.TOKEN_REDIS_DB).then(() => {
      redis.get(key).then((res) => {
        const result: Models.Result = {
          msg: 'ok',
          state: 1,
          results: res,
          fields: [],
        }
        resolve(result)
      })
    })
  })
}

/**
 * 删除token的值
 * @param key
 * @returns
 */
export async function deleteToken(key: string): Promise<Models.Result> {
  return new Promise((resolve) => {
    selectDb(Config.SECURITY.TOKEN_REDIS_DB).then(() => {
      redis.del(key).then((res) => {
        const result: Models.Result = {
          msg: 'ok',
          state: 1,
          results: res,
          fields: [],
        }
        resolve(result)
      })
    })
  })
}

/**
 * 保存验证码
 * @param key 邮件地址/手机号
 * @param smsCode 验证码
 */
export async function saveSmsCode(key: string, smsCode: string): Promise<Models.Result> {
  return new Promise((resolve) => {
    selectDb(Config.SECURITY.SMSCODE_REDIS_DB).then(() => {
      redis.setex(key, Config.SECURITY.SMSCODE_EXPIRES_IN * 60, smsCode).then((res) => {
        const result: Models.Result = {
          msg: 'ok',
          state: 1,
          results: res,
          fields: [],
        }
        resolve(result)
      })
    })
  })
}

/**
 * 保存验证码间隔
 */
export async function saveSmsCodeInterval(key: string): Promise<Models.Result> {
  return new Promise((resolve) => {
    selectDb(Config.SECURITY.SMSCODE_INTERVAL_DB).then(() => {
      redis.setex(key, 60, `${Date.now()}`).then((res) => {
        const result: Models.Result = {
          msg: 'ok',
          state: 1,
          results: res,
          fields: [],
        }
        resolve(result)
      })
    })
  })
}

/**
 * 读取验证码间隔
 */
export async function getSmsCodeInterval(key: string): Promise<Models.Result> {
  return new Promise((resolve) => {
    selectDb(Config.SECURITY.SMSCODE_INTERVAL_DB).then(() => {
      redis.get(key).then((res) => {
        const result: Models.Result = {
          msg: 'ok',
          state: 1,
          results: res,
          fields: [],
        }
        resolve(result)
      })
    })
  })
}
/**
 * 获取邮件验证码
 * @param key 邮件地址
 * @returns
 */
export async function getSmsCode(key: string): Promise<Models.Result> {
  return new Promise((resolve) => {
    selectDb(Config.SECURITY.SMSCODE_REDIS_DB).then(() => {
      redis.get(key).then((res) => {
        const result: Models.Result = {
          msg: 'ok',
          state: 1,
          results: res,
          fields: [],
        }
        resolve(result)
      })
    })
  })
}

/**
 * 删除邮件对应的验证码
 * @param key
 * @returns
 */
export async function deleteSmsCode(key: string): Promise<Models.Result> {
  return new Promise((resolve) => {
    selectDb(Config.SECURITY.SMSCODE_REDIS_DB).then(() => {
      redis.del(key).then((res) => {
        const result: Models.Result = {
          msg: 'ok',
          state: 1,
          results: res,
          fields: [],
        }
        resolve(result)
      })
    })
  })
}
